<?php

/**
 * User
 *  
 * @author Charles
 * @version 
 */

//require_once 'Zend/Db/Table/Abstract.php';
class User_Model_User extends Zend_Db_Table_Row_Abstract
{
    /**
     * The default table name 
     */
    protected $_name = 'users';
    /*
   * Pre-insert logic
   * Automatically insert created_by, date_created &  date_updated
   *
   */
    protected function _insert ()
    {
        if (is_null($this->date_created)) {
            $this->date_created = date('Y-m-d H:i:s');
        }
        if (is_null($this->date_updated)) {
            $this->date_updated = date('Y-m-d H:i:s');
        }
        parent::_insert();
    }
    /**
     * Pre-update logic
     * Automatically update date_updated
     * 
     */
    protected function _update ()
    {
        $this->date_updated = date('Y-m-d H:i:s');
        parent::_update();
    }
    public function createUser ($username, $password, $firstName, $lastName, $role)
    {
        // create a new row
        $rowUser = $this->createRow();
        if ($rowUser) {
            // update the row values
            $rowUser->username = $username;
            $rowUser->password = md5($password);
            $rowUser->first_name = $firstName;
            $rowUser->last_name = $lastName;
            $rowUser->role = $role;
            $rowUser->save();
            //return the new user
            return $rowUser;
        } else {
            throw new Zend_Exception("Could not create user! ");
        }
    }
    public function name ()
    {
        $name = trim($this->first_name . ' ' . $this->last_name);
        if (empty($name)) {
            $name = $this->username;
        }
        return $name;
    }
    public function username ()
    {
        return $this->username;
    }
    function __get ($key)
    {
        //checks if class method exists
        if (method_exists($this, $key)) {
            //calls method and return if method exists
            return $this->$key();
        }
        //class parent's __get) if method doesn't exist
        return parent::__get($key);
    }
}
